package com.clothe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.clothe.dao.IBuycarDAO;
import com.clothe.dto.Clothing;
import com.clothe.dto.UserInfo;

public class BuycarDAOImpl implements IBuycarDAO {
	private Connection conn = null;

	@Override
	public void setConn(Connection conn) {
		this.conn = conn;

	}

	@Override
	public boolean save(Clothing clothing, UserInfo userInfo)
			throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "insert into buycar(buyer_id,clothing_id,clothing_name,price,sale_price,size,amount,color,picture) values(?,?,?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			int i = 1;
			ps.setInt(i++, userInfo.getId());
			ps.setInt(i++, clothing.getId());
			ps.setString(i++, clothing.getName());
			ps.setDouble(i++, clothing.getPrice());
			ps.setDouble(i++, clothing.getSaleprice());
			ps.setString(i++, clothing.getSize());
			ps.setInt(i++, clothing.getAmount());
			ps.setString(i++, clothing.getColor());
			ps.setString(i++, clothing.getPicture());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean delete(UserInfo userInfo,Clothing clothing) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "delete from buycar where buyer_id=? and clothing_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userInfo.getId());
			ps.setInt(2, clothing.getId());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean update(int buyerid,Clothing clothing,int amount) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "update buycar set amount=? where buyer_id=? and clothing_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, amount);
			ps.setInt(2, buyerid);
			ps.setInt(3, clothing.getId());
			if (ps.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public Clothing get(int buyerid) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Clothing clothing = null;
		try {
			String sql = "select * from buycar where buyer_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			rs = ps.executeQuery();
			if (rs.next()) {
				clothing = new Clothing();
				clothing.setId(rs.getInt("clothing_id"));
				clothing.setName(rs.getString("clothing_name"));
				clothing.setPrice(rs.getDouble("price"));
				clothing.setSaleprice(rs.getDouble("sale_price"));
				clothing.setSize(rs.getString("size"));
				clothing.setColor(rs.getString("color"));
				clothing.setAmount(rs.getInt("amount"));
				clothing.setPicture(rs.getString("picture"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return clothing;
	}

	@Override
	public List<Clothing> list(int pageSize, int pageNo,int buyerid) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Clothing clothing = null;
		List<Clothing> clothinglist = null;
		try {
			clothinglist = new ArrayList<Clothing>();
			String sql = "select * from buycar where buyer_id=? limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			ps.setInt(2, (pageNo - 1) * pageSize);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				clothing = new Clothing();
				clothing.setId(rs.getInt("clothing_id"));
				clothing.setName(rs.getString("clothing_name"));
				clothing.setPrice(rs.getDouble("price"));
				clothing.setSaleprice(rs.getDouble("sale_price"));
				clothing.setSize(rs.getString("size"));
				clothing.setAmount(rs.getInt("amount"));
				clothing.setColor(rs.getString("color"));
				clothing.setPicture(rs.getString("picture"));
				clothinglist.add(clothing);

			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();

		}
		return clothinglist;
	}

	@Override
	public int getCount(int buyerid) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select count(*) from buycar where buyer_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public Clothing getbyclothingid(int buyerid, int clothingid)
			throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Clothing clothing = null;
		try {
			String sql = "select * from buycar where buyer_id=? and clothing_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			ps.setInt(2, clothingid);
			rs = ps.executeQuery();
			if (rs.next()) {
				clothing = new Clothing();
				clothing.setName(rs.getString("clothing_name"));
				clothing.setPrice(rs.getDouble("price"));
				clothing.setSaleprice(rs.getDouble("sale_price"));
				clothing.setSize(rs.getString("size"));
				clothing.setColor(rs.getString("color"));
				clothing.setAmount(rs.getInt("amount"));
				clothing.setPicture(rs.getString("picture"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return clothing;
	}

	@Override
	public boolean deleteall(int buyerid) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "delete from buycar where buyer_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public List<Clothing> buycarlist(int buyerid) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		Clothing clothing = null;
		List<Clothing> buycarlist = null;
		try {
			buycarlist = new ArrayList<Clothing>();
			String sql = "select * from buycar where buyer_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, buyerid);
			rs = ps.executeQuery();
			while (rs.next()) {
				clothing = new Clothing();
				clothing.setName(rs.getString("clothing_name"));
				clothing.setId(rs.getInt("clothing_id"));
				clothing.setPrice(rs.getDouble("price"));
				clothing.setSaleprice(rs.getDouble("sale_price"));
				clothing.setSize(rs.getString("size"));
				clothing.setAmount(rs.getInt("amount"));
				clothing.setColor(rs.getString("color"));
				clothing.setPicture(rs.getString("picture"));
				buycarlist.add(clothing);

			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();

		}
		return buycarlist;
	}

}
